/*
 * 文件名：RedisCacheManager.java
 * 版权：Copyright by www.244532032@163.com
 * 描述：
 * 修改人：yangl
 * 修改时间：2016年1月26日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.prisys.redis;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.springframework.cache.Cache;
import org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager;
import org.springframework.data.redis.core.RedisTemplate;

public class RedisCacheManager extends
		AbstractTransactionSupportingCacheManager {
	private ConcurrentMap<String, Cache> cacheMap = new ConcurrentHashMap<String, Cache>();  
	
    private Map<String, Integer> expireMap = new HashMap<String, Integer>(); 
    
    private RedisTemplate<Object, Object> redisTemplate; 
    
    public RedisCacheManager()  
    {   
    }   
  
    @Override  
    protected Collection<? extends Cache> loadCaches()   
    {   
        Collection<Cache> values = cacheMap.values();   
        return values;   
    }

    @Override  
    public RedisCache getCache(String name)   
    {   
    	RedisCache cache = (RedisCache) cacheMap.get(name);   
        if (cache == null)   
        {   
            Integer expire = expireMap.get(name);   
            if (expire == null)   
            {   
                expire = 0;   
                expireMap.put(name, expire);   
            }   
               
            cache = new RedisCache(name, expire.intValue(), redisTemplate);   
            cacheMap.put(name, cache);   
        }   
        return cache;   
    }   
  
    public void setRedisTemplate(
			RedisTemplate<Object, Object> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	public void setConfigMap(Map<String, Integer> configMap)   
    {   
        this.expireMap = configMap;   
    }   

}
